package com.lizhi.yun.core.menu.handler;/**
 * @program: spring-boot
 * @name UppMenuHandle
 * @description:
 * @author: lizhi
 * @create: 2020-04-11 19:03
 */

import com.lizhi.common.yun.entity.ListResponseResult;
import com.lizhi.mybatis.interceptor.PageBounds;
import com.lizhi.yun.db.dao.UppMenuDao;
import com.lizhi.yun.db.dao.UppRoleMenuDao;
import com.lizhi.yun.db.pojo.UppMenuPOJO;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.yun.base.BaseHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 *@program: spring-boot
 *@name UppMenuHandle
 *@description:
 *@author: lizhi
 *@create: 2020-04-11 19:03
 */
@Slf4j
@Service
public class UppMenuHandle extends BaseHandler {

    @Resource
    private UppMenuDao uppMenuDao;

    @Resource
    private UppRoleMenuDao uppRoleMenuDao;

    public ListResponseResult doMain(Map<String, Object> map) {
        PageBounds pageBounds = getPageBounds(map);
        /*查询出所有的一级菜单*/
        /*查询出当前最大等级*/
        Integer menuLevel = uppMenuDao.selectMaxLevelByMap(map);
        if (menuLevel != null){
            map.put("menuLevel", menuLevel);
        }
        List<UppMenuPOJO> listUppMenuDOs = uppMenuDao.selectByMap(map, pageBounds);
        for (UppMenuPOJO uppMenuPOJO : listUppMenuDOs){
            map.remove("menuLevel");
            map.put("parentMenuId", uppMenuPOJO.getMenuId());
            /*查询出其名下的所有子菜单*/
            List<UppMenuPOJO> menuPOJOList = uppMenuDao.selectByMap(map, null);
            /*判断其名下是否存在子菜单*/
            if (menuPOJOList.size() > 0){
                getChildMenu(menuPOJOList, map);
                uppMenuPOJO.setChildren(menuPOJOList);
            } else {
                uppMenuPOJO.setChildren(null);
            }
        }
        return getListResult(pageBounds.getCountRows(), listUppMenuDOs, map);
    }

    /**
     *  循环获取菜单
     * @param list
     * @return
     */
    private void getChildMenu(List<UppMenuPOJO> list, Map<String, Object> map){
        for (UppMenuPOJO uppMenuPOJO : list){
            map.put("parentMenuId", uppMenuPOJO.getMenuId());
            /*查询出其名下的所有子菜单*/
            List<UppMenuPOJO> menuPOJOList = uppMenuDao.selectByMap(map, null);
            if (menuPOJOList.size() > 0){
                uppMenuPOJO.setChildren(menuPOJOList);
                getChildMenu(menuPOJOList, map);
            }
        }
    }

    /**
     *功能描述 根据菜单id查询菜单信息
     * @author lizhi
     * @date 2020/4/12
     * @param menuId:
     * @return com.lizhi.db.upp.pojo.UppMenuPOJO
     */
    public ListResponseResult findById(String menuId) {
        UppMenuPOJO uppMenuPOJO = uppMenuDao.selectById(menuId);
        if (uppMenuPOJO == null) {
            return getListResult(null);
        }
        return getListResult(uppMenuPOJO);
    }

    /**
     *功能描述  新增菜单
     * @author lizhi
     * @date 2020/4/12
     * @param uppMenuPOJO:
     * @return int
     */
    public ListResponseResult insert(UppMenuPOJO uppMenuPOJO) {
        /*判断菜单id是否为空*/
        if (StringUtils.isEmpty(uppMenuPOJO.getMenuId())) {
            return getListResult("菜单id不能为空", false);
        }

        UppMenuPOJO uppMenuPOJO1 = uppMenuDao.selectById(uppMenuPOJO.getMenuId());
        if (!StringUtils.isEmpty(uppMenuPOJO1)){
            return getListResult("菜单ID已存在!", false);
        }

        /*判断父菜单编号，如果为空则为一级菜单*/
        if (StringUtils.isEmpty(uppMenuPOJO.getParentMenuId())){
            uppMenuPOJO.setMenuLevel(1);
        } else {
            /*查询其父菜单为几级菜单*/
            UppMenuPOJO menuPOJO = uppMenuDao.selectById(uppMenuPOJO.getParentMenuId());
            uppMenuPOJO.setMenuLevel(menuPOJO.getMenuLevel() + 1);
        }
        uppMenuPOJO.setIstTm(DateUtils.getNowDate());
        int flag = uppMenuDao.insertRecord(uppMenuPOJO);
        return getListResult(flag);
    }

    /**
     *功能描述 更新菜单
     * @author lizhi
     * @date 2020/4/12
     * @param uppMenuPOJO:
     * @return int
     */
    public ListResponseResult update(UppMenuPOJO uppMenuPOJO){
        /*判断父菜单编号，如果为空则为一级菜单*/
        if (StringUtils.isEmpty(uppMenuPOJO.getParentMenuId())){
            uppMenuPOJO.setMenuLevel(1);
        } else {
            /*查询其父菜单为几级菜单*/
            UppMenuPOJO menuPOJO = uppMenuDao.selectById(uppMenuPOJO.getParentMenuId());
            uppMenuPOJO.setMenuLevel(menuPOJO.getMenuLevel() + 1);
        }
        uppMenuPOJO.setUpdTm(DateUtils.getNowDate());
        int flag = uppMenuDao.updateById(uppMenuPOJO);
        return getListResult(flag);
    }

    /**
     *功能描述 判断当前菜单是否有子菜单
     * @author lizhi
     * @date 2020/4/12
     * @param menuId:
     * @return boolean
     */
    public ListResponseResult isExistsByParentMenuId(String menuId) {
        if (StringUtils.isEmpty(menuId)) {
            return getListResult(false);
        }
        int result = uppMenuDao.countByParentMenuId(menuId);
        return getListResult(result == 0 ? false : true);
    }

    /**
     *功能描述  判断该菜单id是否存在
     * @author lizhi
     * @date 2020/4/12
     * @param menuId:
     * @return boolean
     */
    public ListResponseResult isNotexists(String menuId) {
        UppMenuPOJO uppMenuPOJO = uppMenuDao.selectById(menuId);
        if (null == uppMenuPOJO) {
            return getListResult(true);
        }
        return getListResult(false);
    }

    /**
     *功能描述 删除该菜单
     * @author lizhi
     * @date 2020/4/12
     * @param menuIds:
     * @return int
     */
    public ListResponseResult delete(String menuIds) {
        List<String> listStr = Arrays.asList(menuIds.split(","));
        for (int i = 0; i < listStr.size(); i++){
            String menuId = listStr.get(i);
            uppRoleMenuDao.deleteByMenuId(menuId);
            deleteMenus(menuId);
        }
        return getListResult(uppMenuDao.deleteByList(listStr));
    }

    /**
     * 循环删除该层级下的所有菜单
     * @param menuId
     */
    public void deleteMenus(String menuId){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("parentMenuId", menuId);
        List<UppMenuPOJO> uppMenuPOJOS = uppMenuDao.selectByMap(map, null);
        for (UppMenuPOJO uppMenuPOJO : uppMenuPOJOS){
            uppMenuDao.deleteById(uppMenuPOJO.getMenuId());
            uppRoleMenuDao.deleteByMenuId(uppMenuPOJO.getMenuId());
            deleteMenus(uppMenuPOJO.getMenuId());
        }
    }
}
